﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using Web.UI;
using Model;
using System.Collections;

namespace Web.admin.BasicManage.Majors
{
    public partial class AddMajor : System.Web.UI.Page
    {
        MajorService majorService = new MajorService();
        LocalService localService = new LocalService();
        private static BoMajor majororg;
        AcademyService academyService = new AcademyService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                localList.DataSource = localService.getAllLocal();
                localList.DataTextField = "LOC_NAME";
                localList.DataValueField = "LOC_ID";
                localList.DataBind();
                ACA_List.DataSource = academyService.getAllAcademies();
                ACA_List.DataTextField = "ACA_NAME";
                ACA_List.DataValueField = "ACA_ID";
                ACA_List.DataBind();
            }
            if (!IsPostBack)
            {
                string operation = Request["OPERATION"];
                if (operation == null)
                    return;
                string majorId = Request["MAJ_ID"];
                if (majorId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyMajor(majorId);
                }
                else if (operation == "Delete")
                {
                    DeleteMajor(majorId);
                }
                
            }

        }

        protected void ModifyMajor(string majorId)
        {
            BoMajor major = majorService.GetMajor(majorId);
            MajorIdBOX.Value = majorId;
            TextBoxName.Text = major.MAJ_NAME;
            TextBoxCode.Text = major.MAJ_CODE;
            TextBoxShort.Text =major.MAJ_SHORT ;
            TextBoxEnName.Text = major.MAJ_ENNAME;
            TextBoxCLASSIFICATION.Text =major .MAJ_CLASSIFICATION ;
            TextBoxTEACHER.Text =major .MAJ_TEACHER ;
            KindEditor.Text = major.MAJ_CONTENT;
            TextBoxRemark.Text = major.MAJ_REMARK;
            localList.SelectedValue=major .MAJ_LOCAL ;
            ACA_List.SelectedValue=major .MAJ_ACADEMY ;
            majororg =major;
        }

        protected void DeleteMajor(string majorId)
        {
            BoMajor major = new BoMajor();
            major.MAJ_ID = majorId;
            majorService.DeleteMajor(major);
            Response.Redirect("MajorList.aspx");
        }

        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            BoMajor major = new BoMajor();
            major.MAJ_NAME = TextBoxName.Text;
            major.MAJ_CODE = TextBoxCode.Text;
            major.MAJ_ACADEMY = ACA_List.SelectedValue;
            major.MAJ_CLASSIFICATION = TextBoxCLASSIFICATION.Text;
            major.MAJ_CONTENT = KindEditor.Text;
            major.MAJ_ENNAME = TextBoxEnName.Text;
            major.MAJ_LOCAL = localList.SelectedValue;
            major.MAJ_REMARK = TextBoxRemark.Text;
            major.MAJ_SHORT = TextBoxShort.Text;
            major.MAJ_TEACHER = TextBoxTEACHER.Text;

            BoCheckRepat repeat = new BoCheckRepat();
            repeat.tablename = "Major";
            repeat.constr = "MAJ_IsShow";

            Hashtable keyValue = new Hashtable();
            keyValue.Add("MAJ_NAME", major.MAJ_NAME);
            //keyValue.Add("MAJ_CODE", major.MAJ_CODE);
            keyValue.Add("MAJ_ACADEMY", major.MAJ_ACADEMY);
            keyValue.Add("MAJ_LOCAL", major.MAJ_LOCAL);

            repeat.content = keyValue;
            if (string.IsNullOrEmpty(MajorIdBOX.Value))
            {
                if (checkrepeat.checkrepeatCount(repeat) > 0)
                {
                    Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                }
                else
                {
                    majorService.CreateMajor(major);
                    Response.Redirect("MajorList.aspx");
                }
            }
            else
            {
                if (majororg.MAJ_NAME == major.MAJ_NAME && 
                    majororg.MAJ_ACADEMY == major.MAJ_ACADEMY && 
                    majororg.MAJ_LOCAL==major.MAJ_LOCAL)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                    Response.Redirect("MajorList.aspx");
                }
                else
                {
                    if (checkrepeat.checkrepeatCount(repeat) > 0)
                    {
                        Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                    }
                    else
                    {
                        major.MAJ_ID = MajorIdBOX.Value;
                        majorService.UpdateMajor(major);
                        Response.Redirect("MajorList.aspx");
                    }

                }
            }
        //    if (string.IsNullOrEmpty(ACAIDBOX.Value))
        //    {
        //        BoMajor major = new BoMajor();
        //        major.MAJ_NAME = TextBoxName.Text;
        //        major.MAJ_CODE = TextBoxCode.Text;
        //        major .MAJ_CLASSIFICATION = TextBoxCLASSIFICATION.Text;
        //        major .MAJ_TEACHER = TextBoxTEACHER.Text;
        //        major.MAJ_CONTENT = KindEditor.Text;
        //        major.MAJ_REMARK = TextBoxRemark.Text;
        //        major.MAJ_SHORT = TextBoxShort.Text;
        //        major .MAJ_LOCAL = localList.SelectedValue;
        //        major .MAJ_ACADEMY = ACA_List.SelectedValue;
        //        majorService.CreateMajor(major);
        //        Response.Redirect("MajorList.aspx");
        //    }
        //    else
        //    {
        //        BoMajor major = new BoMajor();
        //        major.MAJ_ID = ACAIDBOX.Value;
        //        major.MAJ_NAME = TextBoxName.Text;
        //        major.MAJ_CODE = TextBoxCode.Text;
        //        major .MAJ_CLASSIFICATION = TextBoxCLASSIFICATION.Text;
        //        major .MAJ_TEACHER = TextBoxTEACHER.Text;
        //        major.MAJ_CONTENT = KindEditor.Text;
        //        major.MAJ_REMARK = TextBoxRemark.Text;
        //        major.MAJ_SHORT = TextBoxShort.Text;
        //        major .MAJ_LOCAL = localList.SelectedValue;
        //        major .MAJ_ACADEMY = ACA_List.SelectedValue;
        //        majorService.UpdateMajor(major);
        //        Response.Redirect("MajorList.aspx");
        //    }
        }
        }
    
}